<?php
/**
 * RegistrationController class file
 * ---------------------------------
 *
 * LICENSE: BSD
 * @copyright  2009 Gemisoft
 * @author Deepak Pradhan <deepak.pradhan@gemisoft.com>
 * @link http://grbac.gemisoft.com/
 * @version $Id$
 * @since 0.1
 */
class RegistrationController extends SBaseController {
/**
 * User Registration and Email Confirmation
 * 1. Resgister
 * 2. Validate
 * 3. Register_passive
 **/

  public function actions() {
    return array('captcha'=>array('class'=>'CCaptchaAction', 'backColor'=>0xF7F9FE,  'testLimit'=>1),);
  }

  /**
   * Registration confirmation/Email veririfation
   */
  public function actionValidate() {
  // intercept params from the URL
    if ( ($_GET['username']) && ($_GET['valid_code']) ) {
      $u = $_GET['username'];
      $v = $_GET['valid_code'];
      $user=Users::model()->find('username=:U AND valid_code=:V', array(':U'=>$u, ':V'=>$v));
      if($user === null) {
        $err = 'No account found for such user and confirmation code, or account is already validated';
        $this->render('validation_failed',array('err'=>$err));	die;
      }
    } else {
      $err = 'Not a valid account confirmation URL';
      $this->render('validation_failed',array('err'=>$err));	die;
    }

    $user->scenario = 'register_validation';
    $user->valid_code = null;
    $user->active = true;
    if($user->save()) {
      $this->render('register_success');
    }
  }


  /**
   * Displays the register page
   */
  public function actionRegister() {
    if(!$this->module->enableRegistration) {
      $this->render('register_disabled');
    }

    $user = new RegisterForm('register');
    $user->scenario='register';

    if(isset($_POST['RegisterForm'])) {
      $user->setAttributes($_POST['RegisterForm'], 'register');
      if($user->validate()) {
        //Pre activate user if email validation is not needed
        $user->active = true;
        if($user->insert()) {
          if($this->module->emailConfirmation) {
          // send an email with confirmation link
            $user->vLink = CController::createAbsoluteUrl('/grbac/registration/validate',array(
                'valid_code'=>$user->valid_code,
                'username'=>$user->username));
            $subject = 'החשבון נוצר - מחכה לאימות';
            $emailText = $this->renderPartial('mail/validate', array('form'=>$user), true, false);
            $this->grbac_sendEmail($to=$user->email, $subject, $emailText);
            $this->render('register_confirm',array('form'=>$user));
            return;
          }
        }
        $this->render('register_success'); //no email confirmation required, so just say thank you
        return;
      }
    }
    // display the registration form
    $this->render('register',array('form'=>$user));
  }

  /**
   * Register passively
   * @param $_POST['email'] must be pre-validated as unique email
   *
   * Usage: only email is needed to register, username & password :=rand
   * 1. set $_POST[email] with email to register
   * 2. invoke runController as: see usage in HR creation
   *    Yii::app()->runController('/grbac/registration/register_passive');
   * 3. send email for user to confirm email, and change username/password
   */
  public function actionRegister_passive() {
    if(!isset($_POST['email'])) return false;

    $user = new RegisterForm('register_passive');
    $user->scenario='register_passive';
    $user->email              = $_POST['email'];
    $user->username           = rand().rand();
    $user->passwordNew        = $user->username;
    $user->passwordNew_repeat = $user->passwordNew;

    if($user->validate()) {
      if($user->insert()) {
        $user->vLink = Yii::app()->request->baseUrl.CController::createUrl('/grbac/registration/validate',array(
            'valid_code'=>$user->valid_code,
            'username'=>$user->username));
        $subject = 'Account created - pending email verification';
        $emailText = $this->renderPartial('mail/validate', array('form'=>$user), true, false);
        $this->grbac_sendEmail($to=$user->email, $subject, $emailText);
      }
    }
    return;
  }

}
